package model.data.tableModel.sale;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import model.data.interfaces.IFinishable;
import model.data.tableModel.TableControl;
import model.data.tableModel.person.Customer;
import model.handler.databaseHandling.QueryFactory;
import enums.Status;

/**
 * Class representing the table bill of the database.
 * It contains information about goods that are bought by one customer per month.
 */
public final class Bill implements IFinishable{

	private final static Logger debug = LogManager.getLogger("DEBUG");
	
	private final int billID;
	private File pdf;
	private String date;
	private double amount;
	private Status status;
	private final Customer customer;
	
	private Bill(final int billID, final File pdf, final String date, final Status status, final Customer customer, final double amount){
		this.billID = billID;
		this.pdf = pdf;
		this.date = date;
		this.status = status;
		this.customer = customer;
		this.amount = amount;
	}
	
	@Override
	public String toString(){
		return "BillID: " + billID + "Date: " +date + " Amount: "+ amount + "Status: " + status.toString() + "Customer: " + customer;
	}
	
	public static Bill createNewBill(final File pdf, final String date, final Status status, final Customer customer, final double amount){
		int id = TableControl.getBillCounter();
		debug.debug("New Bill created ID: " + id);
		return new Bill(id, pdf, date, status, customer, amount);
	}
	
	public static Bill createExistingBill(final int billID, final File pdf, final String date, final Status status, final Customer customer, final double amount){
		debug.debug("Existing Bill created ID: "+ billID );
		return new Bill(billID, pdf, date, status, customer, amount);
	}
	
	@Override
	public boolean writeToDB() {
		return QueryFactory.writeBillToDB(false, billID, pdf, date, customer.getCustomerID(), status, amount);
	}

	@Override
	public boolean writeUpdateToDB() {
		
		return QueryFactory.writeBillToDB(true, billID, pdf, date, customer.getCustomerID(), status, amount);
	}
	
	/* GETTER AND SETTER BELOW */

	public Customer getCustomer() {
		return customer;
	}

	public String getDate() {
		return date;
	}

	public double getAmount() {
		
		return amount;
	}

	public File getFile() {
		return pdf;
	}

	private void setPaid() {
		if(status.equals(Status.ACTIVE)){
			status = Status.PAID;
		} else{
			status = Status.ACTIVE;
		}
	}

	@Override
	public void setStatus(Status status) {
		setPaid();
	}
}
